local sk__sanjue = fk.CreateSkill {

  name = "sk__sanjue",

  tags = { Skill.Compulsory, },

}



sk__sanjue:addEffect("active", {
  name = "sk__sanjue",
  anim_type = "support",
  prompt = "#sk__sanjue",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return #player:getTableMark("@[SkSkills_jilv]sk__sanjue") > 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and table.find(player:getTableMark("@[SkSkills_jilv]sk__sanjue"), function(s)
      return not to_select:hasSkill(s, true)
    end)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local skills = player:getMark("@[SkSkills_jilv]sk__sanjue")
    local choices = table.simpleClone(skills)
    for _, skill in ipairs(target.player_skills) do
      table.removeOne(choices, skill.name)
    end
    local result = room:askForCustomDialog(player, sk__sanjue.name,
      "packages/utility/qml/ChooseSkillBox.qml", {
        choices, 1, 1, "#sk__sanjue-choice::" .. target.id
      })
    local skill = ""
    if result == "" then
      skill = table.random(choices)
    end
    local choice = result
    if #choice > 0 then
      skill = choice[1]
    else
      skill = table.random(choices)
    end
    table.removeOne(skills, skill)
    room:setPlayerMark(player, "@[SkSkills_jilv]sk__sanjue", skills)
    room:handleAddLoseSkills(target, skill, nil, true, false)
  end,
})

sk__sanjue:addEffect(fk.CardUsing, {
  name = "#sk__sanjue_trigger",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(sk__sanjue.name) then
      local n = #player.room.logic:getEventsOfScope(GameEvent.UseCard, 4, function(e)
        local use = e.data
        return use.from and use.from == player and use.card.name == data.card.trueName
      end, Player.HistoryGame)
      return n == 1 or n == 3
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("sk__sanjue")
    room:notifySkillInvoked(player, "sk__sanjue", "drawcard")
    player:drawCards(1, "sk__sanjue")
    if player.dead then return end
    local mark = player:getTableMark("@[SkSkills_jilv]sk__sanjue")
    local skills = {}
    for _, general in ipairs(room.general_pile) do
      local g = Fk.generals[general]
      if g.kingdom == "wu" then
        table.insertTableIfNeed(skills, g:getSkillNameList())
      end
    end
    if #skills == 0 then return end
    local skill = table.random(skills)
    table.insert(mark, skill)
    room:setPlayerMark(player, "@[SkSkills_jilv]sk__sanjue", mark)
  end,
})

sk__sanjue:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "@[SkSkills_jilv]sk__sanjue", 0)
end)

sk__sanjue:addEffect(fk.EventLoseSkill, {
  can_refresh = function(self, event, target, player, data)
    return target == player and data == sk__sanjue
  end,
  on_refresh = function(self, event, target, player, data)
  end,
})

return sk__sanjue
